package com.xiyi.shouxin.controller;

import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.xiyi.shouxin.controller.pojo.ListPiciRecordParams;
import com.xiyi.shouxin.entity.admin.Admin;
import com.xiyi.shouxin.exception.ErrorCode;
import com.xiyi.shouxin.init.CacheUtil;
import com.xiyi.shouxin.service.PiciRecordService;
import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.view.CommonModel;
import com.xiyi.shouxin.view.pojo.ItemPage;
import com.xiyi.shouxin.view.pojo.PiciRecordView;

@CrossOrigin("*")
@RequestMapping("/pici")
@RestController
public class PiciRecordController {
    private Gson gson = new Gson();
    @Autowired
    private Logger logger;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private PiciRecordService piciRecordService;

    @PostMapping("/list_pici")
    public CommonModel listPractice(@RequestBody ListPiciRecordParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        Long adminId = null;
        Admin admin = cacheUtil.getAdminOperator();
        if (admin != null) {
            adminId = admin.getId();
        }
        try {
            ItemPage<PiciRecordView> list = piciRecordService.findPiciRecordList(adminId, params.getStartTime(),
                    params.getEndTime(), params.getPage(), params.getSize());
            return CommonModel.Success(list);
        } catch (Exception e) {
            logger.error("{}查询批次列表{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/del_pici")
    public CommonModel delPici(@RequestBody List<Long> ids) {
        try {
            piciRecordService.delPiciRecord(ids);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("{}删除批次{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(ids), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }
}
